﻿using System;
using System.Data;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class Control_ChiTietGioHang : System.Web.UI.UserControl
{
    double tongtien;
    public void xuatgiohang()
    {
       
        DataTable dt = new DataTable();
        dt = (DataTable)Session["GioHang"];

            if ( Session["GioHang"] != null)
            {
                GridView1.DataSource = dt;
                GridView1.DataBind();
                if (dt.Rows.Count >= 0)
                {
                    for (int i = 0; i < dt.Rows.Count; i++)
                    {
                       ((TextBox)GridView1.Rows[i].FindControl("txtSoLuong")).Text = dt.Rows[i]["SoLuong"].ToString();
                        tongtien += int.Parse(dt.Rows[i]["ThanhTien"].ToString());
                        Session["tongtien"] = tongtien;
                        Label2.Text = tongtien.ToString() + " VND";
                    }
                 }
               
            }
        else
        {
                //neu chua co san pham trong gio hang
                Response.Redirect("~/TrangChu.aspx?GioHangRong=GioHangRong");
        }

    }
        
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            Panel1.Visible = false;
            pnKhongDuTaiKhoan.Visible = false;
            xuatgiohang();
        }
    }
    XuLy xl = new XuLy();

    protected void btnThanhToan_Click(object sender, EventArgs e)
    {// khi dang nhap moi cho thanh toan
       // DataTable dt= new DataTable();
        if (Session["DangNhap"] != null)
        {
            int i;
            int SoChonSp = GridView1.Rows.Count;
            for (i = 0; i < SoChonSp; i++)
            {
                // lay so luong san pham de so sanh
                DataTable dt = xl.Load("select soluongsp from sanpham where masp='" + GridView1.Rows[i].Cells[0].Text + "'");
                int soluongsp = int.Parse(dt.Rows[0][0].ToString());
                int soluongsanphammua = int.Parse(((TextBox)GridView1.Rows[i].FindControl("txtSoLuong")).Text);
                //int soluongcon = 0;
                // xu ly tai khoan khach hang  
                DataTable taikhoan = xl.Load("select taikhoan from khachhang where tendn='" + Session["DangNhap"].ToString() + "'");
                double tienmua = Convert.ToDouble(Session["tongtien"].ToString());
                double taikhoankhach = Convert.ToDouble(taikhoan.Rows[0][0].ToString());
                double tiencon = taikhoankhach-tienmua;
                
                if (soluongsp < soluongsanphammua)// neu het hang
                {
                    Response.Write("<script> alert('Mua Quá Số Lượng Còn! ') ;</script>");
                }
                else if (soluongsp==0)
                {
                    Response.Write("<script> alert('Đã Bán Hết Hàng Vui Lòng CHọn Sản Phẩm Khác! ') ;</script>");
                }
                else if (taikhoankhach < tienmua)
                {
                    pnKhongDuTaiKhoan.Visible = true;
                    Panel1.Visible = false;
                }
                    // kiem tra dieu kien cho san pham da duoc chon mua 

               // else 
                else if ( i ==(SoChonSp-1))
                {
                    string query = "insert into DonHang values('" + DateTime.Now + "','0','" + Session["DangNhap"].ToString() + "','" + Session["tongtien"].ToString() + "')";
                    xl.XuLyDuLieu(query);
                    string query4 = "update Khachhang set taikhoan=" + tiencon + " where tendn='" + Session["DangNhap"].ToString() + "'";
                    xl.XuLyDuLieu(query4);
                    for (int j = 0; j < SoChonSp; j++)
                    {
                        // update don hang va ch tiet donhang
                        
                        DataTable tb1 = new DataTable();
                        tb1 = xl.Load("select MaDonHang from DonHang");

                        int count = tb1.Rows.Count;
                        string madonhang = "";
                        for (int t = 0; t < count; t++)
                        {
                            madonhang = tb1.Rows[t]["MaDonHang"].ToString();
                        }
                        //// update chi tiet don hang cua khach hang
                        //for (int z = 0; z < GridView1.Rows.Count; z++)
                        //{

                            int sl = int.Parse(((TextBox)GridView1.Rows[j].FindControl("txtSoLuong")).Text);
                            //sl = 1;
                            string query2 = "insert into ChiTietDonHang values('" + sl + "','','" + GridView1.Rows[j].Cells[0].Text + "','" + madonhang + "')";
                            xl.XuLyDuLieu(query2);

                        //}
                        DataTable ddt = xl.Load("select soluongsp from sanpham where masp='" + GridView1.Rows[i].Cells[0].Text + "'");
                        int soLuongSP = int.Parse(ddt.Rows[0][0].ToString());
                        int soluongMua = int.Parse(((TextBox)GridView1.Rows[i].FindControl("txtSoLuong")).Text);
                        int soluongcon = 0;
                        soluongcon = soLuongSP - soluongMua;
                        string query3 = "update SanPham set soluongsp=" + soluongcon + " where masp='" + GridView1.Rows[i].Cells[0].Text + "'";
                        xl.XuLyDuLieu(query3);       
                    }
                    // sau khi thoat vog lap moi update tai khoan khach hang
                    Session["GioHang"] = null;
                    Response.Redirect("~/TrangChu.aspx?ThanhToan=ThanhToan");

                }
            }

        }
        else
        {
            Panel1.Visible = true;
            pnKhongDuTaiKhoan.Visible = false;// hien lable cho dang ki hay dang nhap
        }
        // khong dang nhap thi khong cho mua hang
    }
    // xuu ly khi thay doi so luong 
    
    protected void txtSoLuong_TextChanged(object sender, EventArgs e)
    {
        DataTable dt = new DataTable();
        dt = (DataTable)Session["GioHang"];
         for (int i = 0; i < GridView1.Rows.Count; i++)

        {
            
                string maSP = dt.Rows[i][0].ToString();
                int slmua = int.Parse(((TextBox)GridView1.Rows[i].FindControl("txtSoLuong")).Text);
                XuLy xl = new XuLy();
                DataTable ddt = new DataTable();
                ddt = xl.Load("select SoLuongSP from SanPham where MaSP='" + GridView1.Rows[i].Cells[0].Text + "'");//load gia tri so luong hang con lai trong kho 
                int soluongsp = int.Parse(ddt.Rows[0][0].ToString());// xu li so luong trong csdl
                if (slmua > 0 && slmua <= 3)// dat dieu kien cho so luong
                {
                    if (soluongsp >= slmua)
                    {
                        
                        dt.Rows[i][3] = slmua;
                        dt.Rows[i][4] = slmua * int.Parse(dt.Rows[i][2].ToString());// xu ly cot thanh tien 
                    }
                    else
                    {
                        Response.Redirect("BaoLoi.aspx?mod=hasNotProduct");
                    }
                 }
                else
                {
                    Response.Redirect("BaoLoi.aspx?mod=quality");
                }
            
        }
       
       dt.AcceptChanges();
       ///Session["GioHang"] = dt;
        Response.Redirect("~/TrangChu.aspx?GioHang=GioHang");
    }

    //chn xoa san san pham
    protected void GridView1_RowDeleting(object sender, GridViewDeleteEventArgs e)
    {
        DataTable dt = new DataTable();
       
            dt = (DataTable)Session["GioHang"];
            int count = dt.Rows.Count;
            //dt.Rows.RemoveAt(e.RowIndex);
            dt.Rows.RemoveAt(e.RowIndex);// bi loi 
            Session["GioHang"] = dt;
            if (count == 0)
            {
                Response.Redirect("BaoLoi.aspx?mod=emptyCart");
                Session["GioHang"] = null;
            }
            else
            {
           
                Response.Redirect("~/TrangChu.aspx?GioHang=GioHang");
            }
           
    }
}